package org.cttic.visualization.visualization.controller;




import org.apache.http.HttpHost;
import org.cttic.visualization.visualization.common.module.provinceData;
import org.cttic.visualization.visualization.common.sectionBillQuery;
import org.cttic.visualization.visualization.common.trackQueryData;
import org.cttic.visualization.visualization.util.AggregationUtil;
import org.cttic.visualization.visualization.util.VehicleInformationUtil;
import org.cttic.visualization.visualization.util.chargingUtil;
import org.cttic.visualization.visualization.util.itineraryQuery.queryUtil;
import org.cttic.visualization.visualization.util.itineraryQuery.tripUtil;
import org.cttic.visualization.visualization.util.sixUtil.interactiveQueryUtil;
import org.cttic.visualization.visualization.util.trackQuery.trackQuery;
import org.cttic.visualization.visualization.z_service.EmployeeRepository;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;

import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.cardinality.ParsedCardinality;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-05-07 16:03
 */
@RestController
@RequestMapping(value = "/es")
public class ElasticsearchOptionServerController {
    @Autowired
    private chargingUtil chargingUtil;
    @Autowired
    private trackQuery trackQuery;
    @Autowired
    private queryUtil query;
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Autowired
    private AggregationUtil aggregationUtil;
    @Autowired
    private VehicleInformationUtil vehicleInformationUtil;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private interactiveQueryUtil interactiveQueryUtil;
    @RequestMapping(value = "/test2")
    public void GetEsDataStrip(){

        long count_access = interactiveQueryUtil.queryCount_access("36", "2020-05-21 00:00:00", "2020-05-22 00:00:00");
        long count_info = interactiveQueryUtil.queryCount_info("36", "2020-05-21 00:00:00", "2020-05-22 00:00:00");
        System.out.println("access:"+count_access);
        System.out.println("info:"+count_info);
        //产生一个100以内的整数:int x=(int)(Math.random()*9);

//        for (Integer provincel : provinceData.provinceList) {
//            int y=(int)(Math.random()*9);
//            int x=(int)(Math.random()*3)+2;
//            String insertSql = "insert into Accuracy(provinceId,accRatio,respTime) " +
//                    "values(" + provincel + ", 91."+ x+y + ",0." + x+y + ")";
//            System.out.println("insertSql:" + insertSql);
//            jdbcTemplate.update(insertSql);
//        }
//        RestClientBuilder lowLevelRestClient = RestClient.builder(
//                new HttpHost("192.168.114.111", 9200, "http"));
//        RestHighLevelClient client =
////                new RestHighLevelClient(lowLevelRestClient);
//
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        // 搜索方式
//        // matchAllQuery搜索全部
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        boolQueryBuilder.must(QueryBuilders.rangeQuery("tansTime")
//                .gt("2020-05-13 00:00:00")
//                .lt("2020-05-13 00:10:00"));
//        boolQueryBuilder.must(QueryBuilders.rangeQuery("curareacode").gt(110000).lt(119999));
//        boolQueryBuilder.must(QueryBuilders.termQuery("gantryType",1));
////        boolQueryBuilder.must(AggregationBuilders.cardinality("group_by_state").field("vehicleNo"));
////        searchSourceBuilder.aggregation(AggregationBuilders.cardinality("group_by_state")
////                .field("vehicleNo"));
//        searchSourceBuilder.query(boolQueryBuilder);
//
//        // 设置源字段过虑,第一个参数结果集包括哪些字段，第二个参数表示结果集不包括哪些字段
//        //searchSourceBuilder.fetchSource(new String[]{"vehicleNo","tansTime","gantryId"},new String[]{});
//        // 向搜索请求对象中设置搜索源
////        searchRequest.source(searchSourceBuilder);
//        // 执行搜索,向ES发起http请求
//        CountRequest countRequest = new CountRequest("trip_incomplete_202005","trip_info_202005");
//        countRequest.types("_doc");
//        countRequest.source(searchSourceBuilder);
//        CountResponse count = null;
//        try {
//            count = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        System.out.println("查询结果："+count.getCount());
//        String count = aggregationUtil.queryStationCount("2020-05-13 00:00:00", "2020-05-13 00:10:00", 110000, 119999);
//        System.out.println(count);
//        SearchSourceBuilder searchSourceBuilder2 = new SearchSourceBuilder();
//        BoolQueryBuilder boolQueryBuilder2 = QueryBuilders.boolQuery();
//
////        boolQueryBuilder2.must(QueryBuilders.rangeQuery("curareacode").gt(110000).lt(119999));
//
//        boolQueryBuilder2.must(QueryBuilders.prefixQuery("vehiclePlate",provinceData.province.get(11)));
//        searchSourceBuilder2.aggregation(AggregationBuilders.cardinality("qc_vehiclePlate").field("vehiclePlate"));
//        searchSourceBuilder2.query(boolQueryBuilder2);
//        searchSourceBuilder2.size(0);
//
//        SearchRequest searchRequest =new SearchRequest("access_info_202005","access_info_202004","access_info_202003");
//
//        searchRequest.types("_doc");
//
//        searchRequest.source(searchSourceBuilder2);
//        SearchResponse count2 = null;
//        try {
//            count2=restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
////        count2.getAggregations().asMap().values();
////        System.out.println("入网车辆数：***********"+count2);
//        Map<String, Aggregation> stringAggregationMap = count2.getAggregations().asMap();
//        ParsedCardinality group_by_state = (ParsedCardinality) stringAggregationMap.get("qc_vehiclePlate");
//        System.out.println("入网车辆数：***********"+group_by_state.getValue());
//        return group_by_state.getValue()
//        return count.getCount()+"|"+count2.getCount();


    }
}
